package main

import (
	"fmt"
	"log"
	"net"
)

func main() {
	conn, err := net.ListenUDP("udp4", &net.UDPAddr{
		IP:   net.IPv4(0, 0, 0, 0),
		Port: 3000,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	defer conn.Close()
	//conn 广播只能读 不能写
	for {
		// 读取数据
		data := make([]byte, 4096)
		read, remoteAddr, err := conn.ReadFromUDP(data)
		if err != nil {
			fmt.Println("读取数据失败!", err)
			continue
		}
		log.Println(read, remoteAddr)
		fmt.Printf("%s\n\n", data)
	}
}
